#pragma once
#include<iostream>
#include<assert.h>
#include<stdio.h>

using namespace std;

namespace Solution
{
	class string
	{
	public:

		typedef char* iterator;

		iterator begin()
		{
			return _arr;
		}
		iterator begin()const 
		{
			return _arr;
		}

		iterator end()
		{
			return _arr + _size;
		}
		iterator end()const
		{
			return _arr + _size;
		}


		string(const char* arr = "")
		{
			size_t len = strlen(arr);
			_capacity = _size = len;
			_arr = new char[_capacity + 1];
			memmove(_arr, arr, _capacity + 1);
		}

		string(const string& s)
			:_arr(nullptr)
		{
			string tmp(s._arr);
			swap(tmp._arr, _arr);
		}

		string& operator=(const string& s)
		{
			if (s != *this)
			{
				string tmp = s;
				swap(tmp._arr, _arr);
			}

			return *this;
		}

		~string()
		{
			delete[] _arr;
			_capacity = _size = 0;
		}

		const char* c_str()const
		{
			return _arr;
		}

		size_t size()
		{
			return _size;
		}

		size_t capacity()
		{
			return _capacity;
		}
		void clear()
		{
			_size = 0;
			_arr[0] = '\0';

		}
		void reserve(size_t n);
		void append(const char c);
		void append(const string& s);
		void push_back(const char c);
		string& insert(size_t pos, const string& str);
		string& insert(size_t pos, const char c);
		string& erase(size_t pos = 0, size_t len = npos);
		string substr(size_t pos = 0, size_t len = npos) const;
		string& operator+=(const string& s);
		string& operator+=(char c);


		const char& operator[](size_t n)const
		{
			assert(n < _size);
			return _arr[n];
		}

		char& operator[](size_t n)
		{
			assert(n < _size);
			return _arr[n];
		}

		bool operator >(const string& s)const
		{
			return strcmp(c_str(), s.c_str()) > 0;
		}

		bool  operator ==(const string& s)const
		{
			return strcmp(c_str(), s.c_str()) == 0;
		}

		bool operator >=(const string& s)const
		{
			return *this > s || *this == s;
		}

		bool operator <(const string& s)const
		{
			return *this >= s;
		}

		bool operator <=(const string& s)const
		{
			return *this < s || *this == s;
		}


		bool operator!=(const string& s)const
		{
			return !(s == *this);
		}

	private:
		size_t _size;
		size_t _capacity;
		char* _arr;
		const static size_t npos = -1;
	};

	ostream& operator<<(ostream& out, const string& s);
	istream& operator>>(istream& in, string& s);
}
